package cn.fl.mcr.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import cmm.mid.core.framework.dto.DataResultDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrWriteOffConfirmSummaryService;
import cn.fl.mcr.transform.McrWriteOffConfirmSummaryVOTransform;
import cn.fl.mcr.vo.McrWriteOffConfirmSummaryDVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.dto.McrWriteOffConfirmSummaryDTO;
import cn.lionbridgecapital.loan.model.dto.PushWriteOffInfoDTO;
import cn.lionbridgecapital.loan.model.vo.McrWriteOffConfirmSummaryVO;
import cn.lionbridgecapital.loan.service.api.IMcrWriteOffConfirmSummaryApiService;

/**
 * @Description: 核销确认汇总 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrWriteOffConfirmSummaryServiceImpl implements IMcrWriteOffConfirmSummaryService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrWriteOffConfirmSummaryService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrWriteOffConfirmSummaryApiService mcrWriteOffConfirmSummaryApiService;

	@Override
	public McrWriteOffConfirmSummaryVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrWriteOffConfirmSummaryDTO dto = null;//this.自定义ApiService.selectById(id);
		return McrWriteOffConfirmSummaryVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrWriteOffConfirmSummaryVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.mcrWriteOffConfirmSummaryApiService.add(McrWriteOffConfirmSummaryVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffConfirmSummaryApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrWriteOffConfirmSummaryVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrWriteOffConfirmSummaryApiService.modify(McrWriteOffConfirmSummaryVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        if(null == page) {
            page = new Pager();
        }
        BaseLogUtils.info(logger, "McrWriteOffConfirmSummaryServiceImpl", "searchListPage->核销确认参数:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        page = this.mcrWriteOffConfirmSummaryApiService.searchMcrWriteOffConfirmSummaryListPage(page);
        DecimalFormat df = new DecimalFormat("######0.00");
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			List<McrWriteOffConfirmSummaryDVO> sumList = Lists.newArrayList();
			List<McrWriteOffConfirmSummaryDTO> dtoList = (List<McrWriteOffConfirmSummaryDTO>)page.getResultList();
			if(CollectionUtils.isNotEmpty(dtoList)) {
				for(McrWriteOffConfirmSummaryDTO dto:dtoList) {
					McrWriteOffConfirmSummaryDVO vo = new McrWriteOffConfirmSummaryDVO();
					vo.setBatNo(dto.getBatNo());
					vo.setRepayNum(String.valueOf(dto.getRepayNum()));
					vo.setCrtTm(dto.getCrtTm());
					vo.setCrtTm(dto.getCrtTm());
					vo.setCommitType(dto.getCommitType());
					vo.setRepayAmt(df.format(DecimalUtils.convertFenToYuan(dto.getRepayAmt())));
					vo.setRepayPrincipal(df.format(DecimalUtils.convertFenToYuan(dto.getRepayPrincipal())));
					vo.setRepayInterest(df.format(DecimalUtils.convertFenToYuan(dto.getRepayInterest())));
					vo.setRepayPenaltyInterest(df.format(DecimalUtils.convertFenToYuan(dto.getRepayPenaltyInterest())));
					vo.setFailReason(dto.getFailReason());
					sumList.add(vo);
				}
			}
	         page.setResultList(sumList);
		}
		return page;
	}

	/**
	 * 
	 * <p>Title: queryMcrWriteOffConfirmSummaryDetail</p>   
	 * <p>Description: 核销款明细列表</p>   
	 * @param qc
	 * @return   
	 * @see cn.fl.mcr.service.IMcrWriteOffConfirmSummaryService#queryMcrWriteOffConfirmSummaryDetail(cn.lionbridgecapital.loan.model.condition.McrWriteOffConfirmSummaryQC)
	 */
	@Override
	public Pager queryMcrWriteOffConfirmSummaryDetail(Pager page) {
		page = this.mcrWriteOffConfirmSummaryApiService.queryRepayDetailsList(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())) {
			List<McrWriteOffConfirmSummaryVO> voList = McrWriteOffConfirmSummaryVOTransform.toVOList((List<McrWriteOffConfirmSummaryDTO>)page.getResultList());
			 for(McrWriteOffConfirmSummaryVO vo:voList) {
				 vo.setRepayAmt(DecimalUtils.convertFenToYuan(vo.getRepayAmt()).longValue());
	        	 vo.setRepayPrincipal(DecimalUtils.convertFenToYuan(vo.getRepayPrincipal()).longValue());
	        	 vo.setRepayInterest(DecimalUtils.convertFenToYuan(vo.getRepayInterest()).longValue());
	        	 vo.setRepayPenaltyInterest(DecimalUtils.convertFenToYuan(vo.getRepayPenaltyInterest()).longValue());
//	        	 vo.setRepayMoney(DecimalUtils.convertFenToYuan(vo.getRepayMoney()).longValue());
			}
			 page.setResultList(voList);
		}
		return page;
	}

	/**
	 * 
	 * <p>Title: executePushWriteOffConfirmSummar</p>   
	 * <p>Description: 认款推送</p>   
	 * @param pushWriteOffInfoDTO
	 * @return   
	 * @see cn.fl.mcr.service.IMcrWriteOffConfirmSummaryService#executePushWriteOffConfirmSummar(cn.lionbridgecapital.loan.model.dto.PushWriteOffInfoDTO)
	 */
	@Override
	public DataResultDTO executePushWriteOffConfirmSummar(PushWriteOffInfoDTO pushWriteOffInfoDTO) {
		// TODO Auto-generated method stub
		return mcrWriteOffConfirmSummaryApiService.executePushWriteOffConfirmSummar(pushWriteOffInfoDTO);
	}
}
